package com.haier.cloudremote.common;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.content.res.XmlResourceParser;

import com.haier.cloudremote.model.City;
import com.haier.cloudremote.model.HDMIInfo;
import com.haier.cloudremote.model.KeyInfo;
import com.haier.cloudremote.model.Province;
import com.haier.cloudremote.model.Town;

/**
 * 处理XML解析，主要解析城市编码表
 * @author jing
 *
 */
public class XmlParser {

	/**
	 * 获取省列表
	 * @param xrp
	 * @return
	 */
	public static ArrayList<Province> getProvinceList(XmlResourceParser xrp) {
		ArrayList<Province> provinces = new ArrayList<Province>();
		Province province = new Province();
		ArrayList<City> cities = new ArrayList<City>();
		City city = new City();
		ArrayList<Town> towns = new ArrayList<Town>();
		Town town = new Town();
		try {
			// 判断是否到了文件的结尾
			while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
				// 文件的内容的起始标签开始
				if (xrp.getEventType() == XmlResourceParser.START_TAG) {
					String tagname = xrp.getName();
					if (tagname.equals("province")) {
						province.setPid(Integer.parseInt(xrp
								.getAttributeValue(0)));
						province.setPname(xrp.getAttributeValue(1));
					} else if (tagname.equals("city")) {
						city.setCityId(Integer.parseInt(xrp
								.getAttributeValue(0)));
						city.setCityName(xrp.getAttributeValue(1));
					} else if (tagname.equals("town")) {
						town.setTownId(Integer.parseInt(xrp
								.getAttributeValue(0)));
						town.setTownName(xrp.getAttributeValue(1));
					}
					// 文件的内容的结束标签开始
				} else if (xrp.getEventType() == XmlResourceParser.END_TAG) {
					String tagname = xrp.getName();
					if (tagname.equals("province")) {
						province.setCities(cities);
						cities = new ArrayList<City>();
						provinces.add(province);
						province = new Province();
					} else if (tagname.equals("city")) {
						city.setTowns(towns);
						towns = new ArrayList<Town>();
						cities.add(city);
						city = new City();
					} else if (tagname.equals("town")) {
						towns.add(town);
						town = new Town();
					}
				}

				xrp.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			if(e!=null){
				SLog.e("ssc", "XmlParser--------78------e===="+e.getMessage());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			if(e!=null){
				SLog.e("ssc", "XmlParser--------83------e===="+e.getMessage());
			}
		}

		return provinces;
	}
	
	/**
	 * 处理XML解析，解析通用键值表
	 * @param xrp
	 * @return
	 */
	public static ArrayList<KeyInfo> getKeyList(XmlResourceParser xrp, int pageNum){
		ArrayList<KeyInfo> keyInfos = new ArrayList<KeyInfo>();
		KeyInfo keyInfo = new KeyInfo();
		
		try {
        	// 判断是否到了文件的结尾  
			while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { 
			    // 文件的内容的起始标签开始
			    if (xrp.getEventType() == XmlResourceParser.START_TAG) {                  	
			        String tagname = xrp.getName();  
			        if (tagname.equals("key")) {   
//			        	Log.d("ssc", "xrp.getAttributeValue(0)==="+xrp.getAttributeValue(0));
//			        	Log.d("ssc", "xrp.getAttributeValue(0).int==="+Integer.parseInt(xrp.getAttributeValue(0)));
			        	keyInfo.setKeyCode(Integer.parseInt(xrp.getAttributeValue(0)));
			        	keyInfo.setKeyName(xrp.getAttributeValue(1));
			        	keyInfo.setState(0);
			        	keyInfo.setPageNum(pageNum);
			        }
			     // 文件的内容的结束标签开始
			    }else if (xrp.getEventType() == XmlResourceParser.END_TAG) { 
			    	String tagname = xrp.getName();
			    	if(tagname.equals("key")){
			    		keyInfos.add(keyInfo);
			    		keyInfo = new KeyInfo();
			    	}
			    }   
			    
			    xrp.next();  
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			if(e!=null){
				SLog.e("ssc", "XmlParser--------83------e===="+e.getMessage());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			if(e!=null){
				SLog.e("ssc", "XmlParser--------83------e===="+e.getMessage());
			}
		}  
		
		return keyInfos;
		
	}
	
	/**
	 * 处理XML解析，解析数据源
	 * @param xrp
	 * @return
	 */
	public static ArrayList<HDMIInfo> getSignalList(XmlResourceParser xrp){
		ArrayList<HDMIInfo> hdmiInfos = new ArrayList<HDMIInfo>();
		HDMIInfo hdmiInfo = new HDMIInfo();
		
		try {
        	// 判断是否到了文件的结尾  
			while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { 
			    // 文件的内容的起始标签开始
			    if (xrp.getEventType() == XmlResourceParser.START_TAG) {                  	
			        String tagname = xrp.getName();  
			        if (tagname.equals("signal")) {   
			        	hdmiInfo.setHdmiId(Integer.parseInt(xrp.getAttributeValue(0)));
			        	hdmiInfo.setHdmiName(xrp.getAttributeValue(1));
			        	hdmiInfo.setType(xrp.getAttributeValue(2));
			        }
			     // 文件的内容的结束标签开始
			    }else if (xrp.getEventType() == XmlResourceParser.END_TAG) { 
			    	String tagname = xrp.getName();
			    	if(tagname.equals("signal")){
			    		hdmiInfos.add(hdmiInfo);
			    		hdmiInfo = new HDMIInfo();
			    	}
			    }   
			    
			    xrp.next();  
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			if(e!=null){
				SLog.e("ssc", "XmlParser--------83------e===="+e.getMessage());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			if(e!=null){
				SLog.e("ssc", "XmlParser--------83------e===="+e.getMessage());
			}
		}  
		
		return hdmiInfos;
		
	}
}
